/**
 * Copyright 厦门中软海晟信息技术有限公司 版权所有 违者必究 2019
 */
package com.opencvjava.practise;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.springframework.stereotype.Service;

import static com.opencvjava.support.util.CvUtils.imshow;
import static com.opencvjava.support.util.CvUtils.mat;
import static org.opencv.imgproc.Imgproc.THRESH_BINARY;
import static org.opencv.imgproc.Imgproc.THRESH_OTSU;
import static org.opencv.imgproc.Imgproc.TM_CCOEFF_NORMED;
import static org.opencv.imgproc.Imgproc.TM_SQDIFF;
import static org.opencv.imgproc.Imgproc.TM_SQDIFF_NORMED;
import static org.opencv.imgproc.Imgproc.rectangle;
import static org.opencv.imgproc.Imgproc.threshold;

/**
 * @author : sunzb(sunzb@hsit.com.cn)
 * @date: 2019/2/11
 */
@Service
public class Slide {
    public void test() {
        for (int i = 0; i < 6; i++) {
            String path = i == 0 ? "slide.png" : "slide" + i + ".png";
            Mat src = mat("slide", path);
            Mat template = mat("slide", "template.png");
            if (src.empty() || template.empty()) {
                throw new RuntimeException("图片读取失败");
            }
            imshow("源图", src);
            Mat result = new Mat();
            int method = TM_CCOEFF_NORMED;
            Imgproc.matchTemplate(src, template, result, method);

            /*Mat gray = new Mat();
            Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
//        imshow("灰度", gray);
            Mat binary = binary1(gray);
            Mat result = new Mat();
            int method = TM_CCOEFF_NORMED;
            Imgproc.matchTemplate(binary, template, result, method);*/
            Core.MinMaxLocResult minMaxLocResult = Core.minMaxLoc(result);
            Point maxLoc = minMaxLocResult.maxLoc;
            Point minLoc = minMaxLocResult.minLoc;
            Point matchLoc;
            if (method == TM_SQDIFF || method == TM_SQDIFF_NORMED) {
                matchLoc = minLoc;
            } else {
                matchLoc = maxLoc;
            }
            Imgproc.rectangle(src, new Rect(new Point(matchLoc.x, matchLoc.y), new Size(50, 50)), new Scalar(0, 0, 255), 2, 8, 0);
            imshow("结果", src);
        }
    }

    private Mat binary1(Mat gray) {
        Mat binary = new Mat(gray.size(), gray.type());
        int total = (int) gray.total();
        byte[] data = new byte[total];
        gray.get(0, 0, data);
        byte[] result = new byte[total];
        int color;
        for (int i = 0; i < total; i++) {
            color = data[i] & 0xff;
            if (color < 70) {
                result[i] = 0;
            } else {
                result[i] = -1;
            }
        }
        binary.put(0, 0, result);
        imshow("二值化", binary);
        return binary;
    }

    private Mat binary(Mat gray) {
        Mat binary = new Mat();
        threshold(gray, binary, 127, 255, THRESH_BINARY | THRESH_OTSU);
        imshow("二值化", binary);
        return binary;
    }
}
